<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 2017/6/14
 * Time: 下午7:07
 */
class Version extends MY_Admin_Controller
{
    /**
     * 版本列表
     */
    public function index()
    {
        $page = (int)$this->input->get_post('page');
        $page = $page ? $page : 1;

        $limit = 20;
        $offset = ($page-1) * $limit;


        $where = array('status' => ADVERT_IMG_STATUS_NORMAL);


        $this->load->model('common/Model_version','v');
        $count = $this->v->count($where);
        $data  = $this->v->lists($offset,  $limit, $where);


        //分页
        $this->load->library('pagination');

        $config['total_rows'] = $count;
        $config['per_page'] = $limit;
        $config['base_url'] = '/system/Version/index';

        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();


        $data = array
        (
            'data'      => $data,
            'count'     => $count,
            'show_page' => $show_page,
        );


        $this->load->view('admin/system/version/index', $data);
    }


    /**
     * 添加或修改版本
     */
    public function create()
    {
        $vid = $this->uri->segment(4);


        $item = array();
        if ($vid)
        {
            $this->load->model('common/Model_version', 'v');
            $item = $this->v->getById($vid);
        }


        $data = array
        (
            'item' => $item,
        );


        $this->load->view('admin/system/version/create', $data);
    }


    /**
     * 保存版本
     */
    public function save()
    {
        $vid       = (int)$this->input->get_post('vid');
        $name      = trim($this->input->get_post('name'));
        $client    = trim($this->input->get_post('client'));
        $version   = trim($this->input->get_post('version'));
        $release   = trim($this->input->get_post('release'));
        $is_force  = (int)$this->input->get_post('is_force');
        $is_online = (int)$this->input->get_post('is_online');
        //$file         = (int)$this->input->get_post('file');
        $descr     = trim($this->input->get_post('descr'));
        $link      = trim($this->input->get_post('link'));


        $this->load->model('common/Model_version', 'v');


        if ($vid)
        {
            $current_item = $this->v->getById($vid);
            if (empty($current_item))
            {
                show_error('版本不存在!');
            }


            $client = $current_item['client_type'];
        }


        if (empty($name) || !$client || empty($version) || empty($release) || empty($descr))
        {
            show_error('参数不全!');
        }


        if (APP_CLIENT_IOS == $client && empty($link))
        {
            show_error('上架苹果必须添加链接!');
        }


        if (!array_key_exists($is_force, config_item('app_version_is_force')))
        {
            show_error('未知的强制更新状态!');
        }


        if (!array_key_exists($is_online, config_item('app_version_is_online')))
        {
            show_error('未知的上线状态!');
        }


        if (APP_CLIENT_ANDROID == $client && 0 != $_FILES['file']['error'] && !$vid)
        {
            show_error('Android版本必须上传APK包文件!');
        }



        $version_item = $this->v->latest_version($client);
        if (!empty($version_item['data']))
        {
            /*
            if (version_compare($version_item['data']['version'], $version, '!='))
            {
                show_error('该版本号低于当前版本1!');
            }


            if (version_compare($version_item['data']['version'], $version, ($vid ? '!=' : '>')))
            {
                show_error('该版本号低于当前版本!');
            }
            //*/
        }


        $path = 'app'.DS.date('Y-m-d', TIMESTAMP).DS;
        $save_path = ROOTPATH.DS.'static'.DS.$path;
        if(!is_dir($save_path))
        {
            @mkdir($save_path, 0777, true);
        }


        $file_item = array();
        if (0 == $_FILES['file']['error'])
        {
            $config = array
            (
                'upload_path'   => $save_path,
                'allowed_types' => '*',//config_item('allow_upload_filetype'),
                'max_size'      => 64000,//config_item('max_upload_size'),
                'remove_spaces' => true,
                'overwrite'     => false,
            );
            $this->load->library('upload', $config);
            if(!$this->upload->do_upload('file'))
            {
                show_error('APK文件上传失败');
            }


            $file_item = $this->upload->data();
        }



        $item = array
        (
            'name'        => $name,
            'client_type' => $client,
            'version'     => $version,
            'link'        => $link,
            'descr'       => $descr,
            'is_force'    => $is_force,
            'is_online'   => $is_online,
            'release'     => $release,
        );


        if (!empty($file_item))
        {
            $item['link'] = url('static').$path.$file_item['file_name'];
            $item['filename'] = $file_item['file_name'];
            $item['filesize'] = $file_item['file_size'];
        }


        $rs = $this->v->save($item, ($vid ? $vid : null));
        if (!$rs)
        {
            show_error('发布版本失败!');
        }


        redirect(url('admin').'system/Version/index');
    }


    /**
     * 删除版本
     */
    public function delete()
    {
        $vid = (int)$this->uri->segment(4);
        if (!$vid)
        {
            show_error('参数不全!');
        }


        $this->load->model('common/Model_version', 'v');
        $item = $this->v->getById($vid);


        if (empty($item))
        {
            show_error('版本不存在!');
        }


        $rs = $this->v->save(array('status' => ADVERT_IMG_STATUS_DELETE), $vid);
        if (!$rs)
        {
            show_error('删除失败!');
        }


        redirect(url('admin').'system/Version/index');
    }
}